import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import edu.cmu.minorthird.classify.algorithms.svm.SVMLearner;
import edu.cmu.minorthird.classify.sequential.CMMLearner;
import edu.cmu.minorthird.text.Annotator;
import edu.cmu.minorthird.text.MutableTextLabels;
import edu.cmu.minorthird.text.Span;
import edu.cmu.minorthird.text.TextBaseLoader;
import edu.cmu.minorthird.text.TextLabels;
import edu.cmu.minorthird.text.learn.AnnotatorLearner;
import edu.cmu.minorthird.text.learn.BeginContinueEndUniqueReduction;
import edu.cmu.minorthird.text.learn.SequenceAnnotatorLearner;
import edu.cmu.minorthird.text.learn.TextLabelsAnnotatorTeacher;
import etxt2db.features.CharacterFeatureClassifier;
import etxt2db.features.CharacterTypeFeatureClassifier;
import etxt2db.features.EditableTokenFE;
import etxt2db.features.PatternFeatureClassifier;
import etxt2db.features.ValueCaseInsensitiveFeatureClassifier;
import etxt2db.features.ValueCaseSensitiveFeatureClassifier;


public class SolutionMinorthirdCRF {
	private static String trainingSet = "C:\\Users\\Goncalo\\Desktop\\SeminarPartial\\";
	
	public static Annotator getAnnotatorForType(String type,EditableTokenFE fe) throws IOException, ParseException{
		File treino = new File(trainingSet);
		TextBaseLoader loader = new TextBaseLoader();
		loader.load(treino);
		TextLabels treinoLabels = loader.getLabels();
		
		AnnotatorLearner learner = new SequenceAnnotatorLearner(new CMMLearner(new SVMLearner(), 3),fe,new BeginContinueEndUniqueReduction());
		learner.setAnnotationType(type);
		
		TextLabelsAnnotatorTeacher teacher = new TextLabelsAnnotatorTeacher(treinoLabels,type);
		return teacher.train(learner);
	}
	
	public static void main(String[] args) throws IOException, ParseException {
		List<CharacterFeatureClassifier> listFE = new ArrayList<CharacterFeatureClassifier>();
		listFE.add(new CharacterTypeFeatureClassifier());
		listFE.add(new PatternFeatureClassifier());
		listFE.add(new ValueCaseSensitiveFeatureClassifier());
		listFE.add(new ValueCaseInsensitiveFeatureClassifier());
		
		EditableTokenFE fe = new EditableTokenFE(listFE);
		
		List<String> types = new ArrayList<String>();
		types.add("location");
		
		List<Annotator> annotators = new ArrayList<Annotator>();
		for(String type: types){
			annotators.add(getAnnotatorForType(type,fe));
		}
		
		File teste = new File("./resources/test/cmu.andrew.official.cmu-news-2457-0");
		TextBaseLoader loaderTest = new TextBaseLoader();
		loaderTest.load(teste);
		MutableTextLabels testeLabels = loaderTest.getLabels();
		
		for(Annotator ann : annotators){
			ann.annotate(testeLabels);
		}
		
		Iterator<Span> iterDoc = testeLabels.getTextBase().documentSpanIterator();
		while(iterDoc.hasNext()){
			String docName = iterDoc.next().getDocumentId();
			for(String type: types){
				for(Span troco : testeLabels.getTypeSet(type, docName)){
					System.out.println("type: " + troco.asString());
				}
			}
		}
	}

}
